package com.woniu.zsd.recycle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.zsd.recycle.dto.UserSelectPriceDto;
import com.woniu.zsd.recycle.dto.WastePricingBySiteIdDto;
import com.woniu.zsd.recycle.mapper.MaterialTypeMapper;
import com.woniu.zsd.recycle.model.MaterialType;
import com.woniu.zsd.recycle.model.WastePricing;
import com.woniu.zsd.recycle.mapper.WastePricingMapper;
import com.woniu.zsd.recycle.param.StationPriceParam;
import com.woniu.zsd.recycle.param.WasteStationPriceParam;
import com.woniu.zsd.recycle.service.WastePricingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 彭于晏
 * @since 2025年02月18日
 */
@Service
public class WastePricingServiceImpl extends ServiceImpl<WastePricingMapper, WastePricing> implements WastePricingService {

    @Resource
    private WastePricingMapper wastePricingMapper;
    @Resource
    private MaterialTypeMapper materialTypeMapper;
    @Override
    public void StationPrice(StationPriceParam param) throws Exception {
        QueryWrapper<WastePricing> wrapper = new QueryWrapper<>();
        wrapper.eq("material_type_id", param.getMaterialTypeId());
        wrapper.eq("site_id", param.getSiteId());
        WastePricing wastePricing = wastePricingMapper.selectOne(wrapper);
        if (wastePricing != null) {
            wastePricing.setPrice(param.getPrice());
            wastePricingMapper.updateById(wastePricing);
        } else {
            WastePricing pricing = BeanUtil.toBean(param, WastePricing.class);
            wastePricingMapper.insert(pricing);
        }

    }

    @Override
    public List<WastePricingBySiteIdDto> selectStationPriceBySiteId(Integer siteId) throws Exception {
        List<WastePricingBySiteIdDto> dtos=wastePricingMapper.selectStationPriceBySiteId(siteId);
        System.out.println(dtos);
        return dtos;
    }

    @Override
    public List<WastePricingBySiteIdDto> selectPriceByTypeId(String typeName) throws Exception {
        List<WastePricingBySiteIdDto> dtos=wastePricingMapper.selectPriceByTypeId(typeName);
        return dtos;
    }

    @Override
    public void wasteStationPrice(WasteStationPriceParam param) throws Exception {
        MaterialType materialType = materialTypeMapper.selectById(param.getMaterialTypeId());
        if (materialType == null) {
            throw new Exception("类型不存在");
        }
        if (materialType.getStatus() != 0) {
            throw new Exception("该类型已被禁用或删除");
        }
        if (materialType.getTypeSelf() == 1){
            throw new Exception("该类型为父类型，无法设置价格");
        }
        QueryWrapper<WastePricing> wrapper = new QueryWrapper<>();
        wrapper.eq("material_type_id", param.getMaterialTypeId());
        wrapper.eq("site_id", 0);
        WastePricing wastePricing = wastePricingMapper.selectOne(wrapper);
        if (wastePricing != null) {
            wastePricing.setPrice(param.getPrice());
            wastePricingMapper.updateById(wastePricing);
        } else {
            WastePricing pricing = new WastePricing();
            pricing.setMaterialTypeId(param.getMaterialTypeId());
            pricing.setPrice(param.getPrice());
            pricing.setSiteId(0);
            wastePricingMapper.insert(pricing);
        }
        //查询所有站点
        List<Integer> list = wastePricingMapper.selectStationIds();
        for (Integer siteId : list) {
            if(siteId != 0|| siteId != null){
                WastePricing pricing = new WastePricing();
                pricing.setMaterialTypeId(param.getMaterialTypeId());
                pricing.setPrice(param.getPrice());
                pricing.setSiteId(siteId);
                wastePricingMapper.insert(pricing);
            }
        }

    }

    @Override
    public List<UserSelectPriceDto> userSelectPrice() {
        List<UserSelectPriceDto> dtos = wastePricingMapper.userSelectPrice();
        return dtos;
    }

    @Override
    public void updatePriceByStationId(Integer stationId) throws Exception {
        QueryWrapper<WastePricing> wrapper = new QueryWrapper<>();
        wrapper.eq("site_id", 0);
        List<WastePricing> list = wastePricingMapper.selectList(wrapper);
        for (WastePricing wastePricing : list) {
            WastePricing pricing = new WastePricing();
            pricing.setPrice(wastePricing.getPrice());
            pricing.setMaterialTypeId(wastePricing.getMaterialTypeId());
            pricing.setSiteId(stationId);
            wastePricingMapper.insert(pricing);
        }
    }
}
